package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.PlaceService;

/**
 * Servlet implementation class UpdateEvent
 */
@WebServlet("/UpdateEventServlet")
public class UpdateEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateEventServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	
	 
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");  
		PrintWriter out = response.getWriter();  
		HttpSession session=request.getSession(true);
		
		String namevent=request.getParameter("namevent");
		int price=Integer.parseInt(request.getParameter("price"));
		String description=request.getParameter("descriptions");
		int idplace=Integer.parseInt(request.getParameter("idplace"));
		int seats=Integer.parseInt(request.getParameter("seats"));
		int idevent = Integer.parseInt(request.getParameter("idevent"));

		try{
			EventService es=new EventService();	
			Event event=new Event();

			PlaceService ps = new PlaceService();
			Place place = ps.viewPlace(idplace);
			
			event.setiDevent(idevent);
			event.setNamevent(namevent);  
	        event.setPrice(price); 
	        event.setDescription(description); 
	        event.setSeats(seats);
	        event.setPlace(place);
    
	       
	        
	      	boolean tt =es.updateEvent(event,idevent);
	 		    if (tt==true){	
	 		    RequestDispatcher rd=request.getRequestDispatcher("mainpage2.jsp");  
	 		    rd.forward(request,response);
	 		    
	 		   }else{
	 		    	 out.print("The event was not been created correctly");  
	 		         RequestDispatcher rd2=request.getRequestDispatcher("neweventregistration.jsp");  
	 		         rd2.forward(request,response);  
	 		         }
	 	    }catch(Exception ex){
	 			ex.getMessage();
	 		}     
	 		    out.close();  
		
	}

}
